#!/usr/bin/env python
# -*- coding: UTF-8 -*-


import traceback
import sys
from optparse import OptionParser

reload(sys)
sys.setdefaultencoding('utf8')

def main():
    parser = OptionParser()
    parser.add_option('-f', '--func', dest='func', help='the exec function')
    parser.add_option('-a', '--api', dest='api', help='the api name')
    parser.add_option('--adapter', dest='adapter', help='the adapter name')
    parser.add_option('-d', '--dir', dest='dir', help='the api scan dir')
    #parser.add_option('-e', '--env', type="choice", dest='env', choices=['online','downline'], help='the environment [online or downline] (default:online)', default='online')
    #parser.add_option('-d', '--detail', action='store_true', dest='detail', help='print detail output')
    #parser.add_option('-o', '--output', action='store_const', const='relation.log', dest='output', help='write output to file')
    options, _ = parser.parse_args()
    function_main(options.func,vars(options))

def function_main(action,params):
    funcs = globals().get(action, None)
    if funcs:
        funcs(params)
    else:
        function_autoExec(action,params)
        #print 'the funcs is not exists : ',action

def function_autoExec(action,params):
    model=None
    try:
        model=__import__("%s.%s" % ('core',action))
    except:
        print 'the funcs is not exists : ',action,traceback.print_exc(5)
    if model:
        cld_model=getattr(model,action)
        Process=getattr(cld_model,'Process')
        create = Process()
        create.main(params)

if __name__ == '__main__':
    main()
